package com.minhua.jijiehao2.mainframe.creditcard.creditcardfragment.impl

import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import com.bigkoo.convenientbanner.ConvenientBanner
import com.bumptech.glide.Glide
import com.minhua.jijiehao2.R
import com.minhua.jijiehao2.base.BaseFragment
import com.minhua.jijiehao2.base.adapter.ViewHolder
import com.minhua.jijiehao2.base.adapter.recyclerview.BaseCommonAdapter
import com.minhua.jijiehao2.bean.CreditCardBean
import com.minhua.jijiehao2.mainframe.common.webviewactivity.impl.WebActivity
import com.minhua.jijiehao2.mainframe.creditcard.creditcardfragment.imp.ICreditCardView
import com.minhua.jijiehao2.mainframe.launcher.loginactivity.impl.LoginActivity
import com.minhua.jijiehao2.utils.CountUtils
import com.minhua.jijiehao2.utils.LocalImgBannerHolder
import kotlinx.android.synthetic.main.fragment_credit_card.*

class CreditCardFragment : BaseFragment<ICreditCardView, CreditCardPresenter>(), ICreditCardView {

    override fun getLayoutId(): Int = R.layout.fragment_credit_card

    override fun getPresenter(): CreditCardPresenter = CreditCardPresenter()


    companion object {
        fun newInstance(): CreditCardFragment {
            val fragment = CreditCardFragment()
            val args = Bundle()
            fragment.arguments = args
            return fragment
        }
    }

    private val banner = arrayListOf<Int>()

    private val topList = arrayListOf<CreditCardBean>()
    private var topAdapter: BaseCommonAdapter<CreditCardBean>? = null

    private val normalList = arrayListOf<CreditCardBean>()
    private var normalAdapter: BaseCommonAdapter<CreditCardBean>? = null


    override fun initView() {
        initBanner()
        initTop()
        initNormal()
    }

    override fun initData() {
        mPresenter.getTopList()
        mPresenter.getNormalList()
    }

    private fun initNormal() {
        rvCardNormal.isNestedScrollingEnabled = false
        rvCardNormal.layoutManager = GridLayoutManager(activity, 2, LinearLayoutManager.VERTICAL, false)
        normalAdapter = object : BaseCommonAdapter<CreditCardBean>(activity, R.layout.item_card_normal, normalList) {
            override fun convert(holder: ViewHolder, creditCardBean: CreditCardBean) {
                holder.setText(R.id.tvItemNormalName, creditCardBean.bankName)
                holder.setText(R.id.tvitemNormalInfo, creditCardBean.message)
                Glide.with(activity).load(creditCardBean.logoUrl).into(holder.getView(R.id.ivItemNormal))
                holder.setOnClickListener(R.id.llItemView) {
                    if (LoginActivity.logged(activity)) {
                        CountUtils.recodeDetail(CountUtils.CARD, creditCardBean.id.toString())
                        WebActivity.intentStart(activity, creditCardBean.skipUrl, creditCardBean.bankName)
                    }
                }
            }
        }
        rvCardNormal.adapter = normalAdapter

    }

    private fun initTop() {
        rvCardTop.isNestedScrollingEnabled = false
        rvCardTop.layoutManager = GridLayoutManager(activity, 3, LinearLayoutManager.VERTICAL, false)
        topAdapter = object : BaseCommonAdapter<CreditCardBean>(activity, R.layout.item_card_top, topList) {
            override fun convert(holder: ViewHolder, creditCardBean: CreditCardBean) {
                holder.setText(R.id.tvItemTopName, creditCardBean.bankName)
                Glide.with(activity).load(creditCardBean.logoUrl).into(holder.getView(R.id.ivItemTop))
                holder.setOnClickListener(R.id.llItemView) {
                    if (LoginActivity.logged(activity)) {
                        CountUtils.recodeDetail(CountUtils.CARD, creditCardBean.id.toString())
                        WebActivity.intentStart(activity, creditCardBean.skipUrl, creditCardBean.bankName)
                    }
                }
            }
        }
        rvCardTop.adapter = topAdapter
    }

    private fun initBanner() {
        cbBanner
                //设置指示器是否可见
                .setPointViewVisible(true)
                //设置自动切换（同时设置了切换时间间隔）
                .startTurning(2000)
                //设置两个点图片作为翻页指示器，不设置则没有指示器，可以根据自己需求自行配合自己的指示器,不需要圆点指示器可用不设
                .setPageIndicator(intArrayOf(R.mipmap.guide_point, R.mipmap.guide_point_cur))
                //设置指示器的方向（左、中、右）
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL).isManualPageable = true
        cbBanner.setPages({ LocalImgBannerHolder() }, banner)
        banner.add(R.mipmap.pagebanner)
        banner.add(R.mipmap.pagebanner)
        banner.add(R.mipmap.pagebanner)
        cbBanner.notifyDataSetChanged()
    }


    override fun onTopSuccess(list: List<CreditCardBean>) {
        if (list.isNotEmpty()) {
            topList.clear()
            topList.addAll(list)
            topAdapter?.notifyDataSetChanged()
        }
    }

    override fun onNormalSuccess(list: List<CreditCardBean>) {
        if (list.isNotEmpty()) {
            normalList.clear()
            normalList.addAll(list)
            normalAdapter?.notifyDataSetChanged()
        }
    }
}
